package com.jnu.hospitalexamination.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jnu.hospitalexamination.common.Result;
import com.jnu.hospitalexamination.entity.Cidetailedreport;
import com.jnu.hospitalexamination.entity.Cireport;
import com.jnu.hospitalexamination.entity.vo.CireportVO;
import com.jnu.hospitalexamination.service.CidetailedreportService;
import com.jnu.hospitalexamination.service.CireportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cireport")
public class CireportController {
    @Autowired
    private CireportService cireportService;
    @Autowired
    private CidetailedreportService cidetailedreportService;

    /**
     * 根据订单id查询报告检查项和检查项明细
     * @param orderId
     * @return
     */
    @RequestMapping("/getReportDetails")
    public Result getReportDetails(Integer orderId) {
        try {
            List<CireportVO> cireportVOList = cireportService.SelectByOrderId(orderId);
            for (CireportVO cireportVO : cireportVOList) {
                cireportVO.setCidrList(cidetailedreportService.SelectByCiId(cireportVO.getCiId()));
            }
            return Result.success(200, "查询成功", cireportVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "查询失败"+e.getMessage(), null);
        }
    }

    /**
     * 根据cidrid更新报告检查项明细的值
     * @param cidrid
     * @param value
     * @return
     */
    @Transactional
    @RequestMapping("/updateReportDetails")
    public Result updateReportDetails(Integer cidrid, String value) {
        try {
            LambdaUpdateWrapper<Cidetailedreport> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Cidetailedreport::getCidrid, cidrid).set(Cidetailedreport::getValue, value);
            boolean update = cidetailedreportService.update(wrapper);
            if (update){
                return Result.success(200, "更新成功", null);
            }else {
                return Result.error(500, "更新失败", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(500, "更新失败"+e.getMessage(), null);
        }
    }
}
